package cn.legend.travel.admin.destination.dao.persist.repository.impl;

import cn.legend.travel.admin.destination.dao.persist.mapper.DistrictMapper;
import cn.legend.travel.admin.destination.dao.persist.repository.IDistrictRepository;
import cn.legend.travel.admin.destination.pojo.entity.District;
import cn.legend.travel.admin.destination.pojo.param.DistrictAddNewParam;
import cn.legend.travel.admin.destination.pojo.vo.DistrictAdminVO;
import cn.legend.travel.common.pojo.po.DistrictSimplePO;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class DistrictRepositoryImpl implements IDistrictRepository {
    private final DistrictMapper mapper;

    @Override
    public List<DistrictSimplePO> getByParent(long pId) {
        return mapper.getByParent(pId);
    }

    @Override
    public int delete(Long id) {
        return mapper.deleteById(id);
    }

    @Override
    public PageData<DistrictAdminVO> listByParent(Long pId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return PageInfoToPageDataConverter.convert(new PageInfo<>(mapper.listByParent(pId)));
    }

    @Override
    public int updateById(District district) {
        return mapper.updateById(district);
    }

    @Override
    public int addNew(DistrictAddNewParam param) {
        District district = new District();
        BeanUtils.copyProperties(param, district);
        return mapper.insert(district);
    }

}
